<template>
	<div class="coursewareComment w944">
		<alert ref="alert"></alert>
		<deleteAlert ref="deleteAlert"></deleteAlert>
		<div class="label_top">
			<p>{{courseName}}</p>
			<em @click="reBack">返回</em>
		</div>
		<div class="label_table">
			<el-table ref="multipleTable" :data="tableData" border @selection-change="handleSelectionChange">
				<el-table-column type="selection"></el-table-column>
				<el-table-column label="用户名">
					<template slot-scope="scope"><img src="../../assets/qudaoImg/anonymous.png" v-if="scope.row.cryptonym==1">{{ scope.row.nickname}}</template>
				</el-table-column>
				<el-table-column prop="discuss" label="评论"></el-table-column>
				<el-table-column label="操作">
					<template slot-scope="scope">
						<el-button @click="handleDelete(scope.row)" class="red">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="fenyeMargin">
				<el-button @click="toggleSelection(tableData)" class="allSelect">全选</el-button>
				<el-button @click="handleDeleteAll" class="allDelete">批量删除</el-button>
				<div class="pag_block">
					<el-pagination  @current-change="handleCurrentChange" :current-page="currentpage" :page-size="pagesize" layout="prev, pager, next" :total="total"></el-pagination>
				</div>
			</div>
		</div>
	</div>
</template>
<script>
	import alert from '../../components/Alert.vue';
	import deleteAlert from '../../components/deleteAlert.vue';
	export default {
		data() {
			return {
				courseid:'',
				coursewareId:'', //课程编号id
				courseName:'', //课程名称
				coursewareType:'',//類型
				nowBack:'',
				teacherTitle:'',
				lecturerid:'',
		        pagesize: 15,
		        currentpage: 1,
		        total: 1,
				id:'',
				tableData: [],
				tableChecked:[],
				ids:[],
				multipleSelection: [],
				deleteType:'delete'
			}
		},
		methods: {
			getParams() {
				let that = this;
				this.courseid = this.$route.params.courseid;
				this.nowBack = this.$route.params.nowBack;
				this.coursewareId = this.$route.params.coursewareId;
				this.courseName = this.$route.params.courseName;
				this.coursewareType = this.$route.params.coursewareType;
				this.updateTitle = this.$route.params.updateTitle;
				this.pathKey = this.$route.params.pathKey;
				this.teacherTitle = this.$route.params.teacherTitle;
				this.lecturerid = this.$route.params.lecturerid;
				this.totalComment = this.$route.params.totalComment;
				console.log('courseWareComment:',this.$route.params);
			},
			getTableList(){
				let that = this;
				if(this.totalComment==1){
					this.$ajax({
						url: this.$root.jifuUrl + "/mns/coursewareGroupReview/getReviewByCourseid",
						headers:{'ContentType': 'application/json; charset=utf-8'},
						method: 'POST',
						data: {
							courseId:this.courseid,
							page:this.currentpage,
							num:this.pagesize
						},
						success: res => {
							if(res.data.code == 200) {
								this.tableData = res.data.data.list;
								this.total=res.data.data.pageParam.items;
							}
						}
					});
				}else{
					this.$ajax({
						url: this.$root.jifuUrl + "/mns/coursewareDiscussChapter/coursewareDiscussChapterList",
						headers:{'ContentType': 'application/json; charset=utf-8'},
						method: 'POST',
						data: {
							coursewareId:this.courseid,
							page:this.currentpage,
							num:this.pagesize,
							coursewareType:this.coursewareType
						},
						success: res => {
							if(res.data.code == 200) {
								this.tableData = res.data.data.list;
								this.total=res.data.data.pageParam.items;
							}
						}
					});
				}
			},
			toggleSelection(rows) {
				if(rows) {
					rows.forEach(row => {
						this.$refs.multipleTable.toggleRowSelection(row, !this.allSelect)
					})
					this.allSelect = !this.allSelect;
				}
			},
			handleSelectionChange(val) {
				this.multipleSelection = [];
				val.forEach(row => {
					this.multipleSelection.push(row.id);
				});
			},
			handleDelete(row){
				this.transitionRow = row;
				this.$rootFunction().rootDelete();
			},
			handleDeleteAll() {
				this.$rootFunction().rootDeleteAll();
			},
			deleteSubmit() {
				if(this.deleteType == 'delete'){
					var list = [];
					list.push(this.transitionRow.id);
					this.$ajax({ 
						url: this.$root.jifuUrl + "/mns/coursewareDiscussChapter/deleteCoursewareDiscussChapter",
						headers:{'ContentType': 'application/json; charset=utf-8'},
						method: 'DELETE',
						data:list,
						success: res => {
							if (res.data.code == 200) {
								this.$rootFunction().deleteResSuccess();
							}else{
								this.failText = res.data.message;
								this.$rootFunction().deleteResFail();
							}
						}
					});
				}else{
					this.$ajax({ 
						url: this.$root.jifuUrl + "/mns/coursewareDiscussChapter/deleteCoursewareDiscussChapter",
						headers:{'ContentType': 'application/json; charset=utf-8'},
						method: 'DELETE',
						data:this.multipleSelection,
						success: res => {
							if (res.data.code == 200) {
								this.$rootFunction().deleteResSuccess();
							}else{
								this.failText = res.data.message;
								this.$rootFunction().deleteResFail();
							}
						}
					});
				}
			},
			handleCurrentChange(value) {
				this.currentpage = value;
				this.getTableList();
			},
			reBack(){
				console.log('this.nowBack:',this.nowBack);
				if(this.nowBack==3){
					this.$router.push({
						name: 'teacherHomework',
						params:{
							updateTitle:this.teacherTitle,
							speakerId:this.lecturerid
						}
					})
				}else{
					this.$router.push({
						name: 'courseware',
						params: {
							liveId:this.coursewareId,
							updateTitle:this.updateTitle,
							pathKey:this.pathKey,
							teacherTitle:this.teacherTitle,
							lecturerid:this.lecturerid,
							nowBack:this.nowBack
						}
					})
				}
			}
		},
		components:{
			alert,
			deleteAlert
		},
		mounted: function() {
			this.getParams();
			if(this.courseid==null){
				this.$store.commit('GET_courseComment');
				var s =this.$store.state.courseComment;
				this.courseid = s.courseid,
				this.coursewareId=s.coursewareId,
				this.courseName=s.courseName;
				this.coursewareType=s.coursewareType;
				this.updateTitle = s.updateTitle;
				
				this.teacherTitle = s.teacherTitle;
				this.lecturerid = s.lecturerid;
				this.totalComment = s.totalComment;
				this.nowBack = s.nowBack;
				this.pathKey = s.pathKey;
			}
			this.getTableList();
			window.addEventListener("beforeunload",()=>{
				s={
					courseid:this.courseid,
					coursewareId : this.coursewareId,
					courseName:this.courseName,
					coursewareType:this.coursewareType,
					updateTitle:this.updateTitle,
					
					teacherTitle:this.teacherTitle,
					lecturerid:this.lecturerid,
					totalComment:this.totalComment,
					nowBack:this.nowBack,
					pathKey:this.pathKey
				}	
				this.$store.commit('SET_courseComment', s);
			});
		}
	}
</script>